package nl.rainbowsheep.reisorg.empl.site.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import javax.validation.Valid;
import nl.rainbowsheep.reisorg.empl.site.model.City;
import nl.rainbowsheep.reisorg.empl.site.model.Country;
import nl.rainbowsheep.reisorg.empl.site.model.Region;
import nl.rainbowsheep.reisorg.empl.site.service.CityService;
import nl.rainbowsheep.reisorg.empl.site.service.CountryService;
import nl.rainbowsheep.reisorg.empl.site.service.RegionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Autowired
    CountryController countrycontroller;
    
    @Autowired
    CountryService countryservice;

    
    @Autowired
    RegionController regioncontroller;
    
    @Autowired
    RegionService regionservice;

    
    @Autowired
    CityController citycontroller;
    
    @Autowired
    CityService cityservice;

    private static final Logger LOGGER = LoggerFactory.getLogger(DestinationController.class);

    @RequestMapping(value = {"/", "/index", "", "/overview"})
    public ModelAndView getAll() {
        List<Country> countries = countrycontroller.getAllCountries();
        ModelAndView model = new ModelAndView("destination/overview");
        model.addObject("countries", countries);
        return model;
    }
    
    @RequestMapping(value = "/country", method = RequestMethod.GET)
    public ModelAndView createCountry() {
        ModelAndView mav = new ModelAndView("destination/country");
        mav.addObject("country", new Country());
        return mav;
    }

    @RequestMapping(value = "/country", method = RequestMethod.POST)
    public String createCountry(@ModelAttribute("country") Country country, BindingResult bindingResult) {
        country.setDescription("");
        countryservice.insertCountry(country);
        return "redirect:overview";
    }

    @RequestMapping(value = "/region", method = RequestMethod.GET)
    public ModelAndView createRegion() {
        ModelAndView mav = new ModelAndView("destination/region");
        return mav;
    }
    
    @RequestMapping(value = "/region/{id}", method = RequestMethod.GET)
    public ModelAndView createRegion(@PathVariable long id) {
        ModelAndView mav = new ModelAndView("destination/region");
        Region region = new Region();
        region.setCountryId(id);
        mav.addObject("region", region);
        return mav;
    }

    @RequestMapping(value = "/region/{countryId}", method = RequestMethod.POST)
    public String createRegion(@ModelAttribute("region") Region region, @PathVariable long countryId, BindingResult bindingResult) {
        region.setDescription("");
        region.setCountryId(countryId);
        regionservice.insertRegion(region);
        return "redirect:/destination/overview";
    }
    
    @RequestMapping(value = "/city", method = RequestMethod.GET)
    public ModelAndView createCity() {
        ModelAndView mav = new ModelAndView("destination/city");
        return mav;
    }

    @RequestMapping(value = "/city/{id}", method = RequestMethod.GET)
    public ModelAndView createCity(@PathVariable long id) {
        ModelAndView mav = new ModelAndView("destination/city");
        City city = new City();
        city.setRegionId(id);
        mav.addObject("city", city);
        return mav;
    }

    @RequestMapping(value = "/city/{regionId}", method = RequestMethod.POST)
    public String createCity(@ModelAttribute("city") City city, @PathVariable long regionId, BindingResult bindingResult) {
        city.setDescription("");
        city.setRegionId(regionId);
        Region region = regionservice.getByRegionId(regionId);
        city.setCountryId(region.getCountryId());
        cityservice.insertCity(city);
        return "redirect:/destination/overview";
    }
    

//    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
//    public ModelAndView getCountry(@PathVariable long id) {
//        Country country = countryservice.getById(id);
//        ModelAndView mav = new ModelAndView("country/details");
//        mav.addObject("country", country);
//        return mav;
//    }
//
//    @RequestMapping(value = "/create", method = RequestMethod.GET)
//    public ModelAndView getDestinationForm() {
//        ModelAndView mav = new ModelAndView("country/create");
//        mav.addObject("country", new Country());
//        return mav;
//    }
//
//    /**
//     *
//     * @param country
//     * @param bindingResult
//     * @return
//     */
//    @RequestMapping(value = "/create", method = RequestMethod.POST)
//    public String createCountry(@ModelAttribute("country") @Valid Country country, BindingResult bindingResult) {
//        countryservice.insertCountry(country);
//        // moet nog
//        return "country/create";
//    }
}
